//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    boardContent : "暂无公告说明",
    hrefUrl: "",
    hrefDesc: ""
  },

  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../jimeng/jimeng'
    })
  },

  bindVoice: function(){
    wx.navigateTo({
      url: '../jimengvoice/jimengvoice'
    })
  },

  showBoard: function(){
    wx.showModal({
      title: '公告说明',
      content: this.data.boardContent,
      showCancel : false
    })
  },

  onShareAppMessage: function (res) {
    return {
      title: '解梦师',
      path: '/pages/index/index',
      success: function (res) {
        // 转发成功
        wx.showToast({
          title: '转发成功',
          icon: 'success',
          duration: 1200
        })
      },
      fail: function (res) {
        // 转发失败
        wx.showToast({
          title: '转发失败',
          icon: 'loading',
          duration: 1200
        })
      }
    }
  },

  onLoad: function () {
    //显示转发
    wx.showShareMenu({
      withShareTicket: true
    })

    console.log("start")
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
      console.log(1)
      this.login()
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
          console.log(3)
          this.login()
        }
      })
    }
    console.log("end")
  },

  getUserInfo: function (e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },

  login: function () {
    //if (wx.getStorageSync('sessionkey')) return;
    var that = this;
    // 登录注册用户信息并获取sessionId
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        console.log(res.code)
        wx.request({
          url: 'https://api.jiemengshi.com/jms/wechat/user/login/code',
          data: {
            code: res.code,
            nickName: that.data.userInfo.nickName,
            avatarUrl: that.data.userInfo.avatarUrl,
            gender: that.data.userInfo.gender,
            province: that.data.userInfo.province,
            city: that.data.userInfo.city,
            country: that.data.userInfo.country,
          },
          success: function (res) {
            console.log(res)
            wx.setStorageSync('sessionkey', res.data.data.sessionId)
          }
        })
      },

      fail: function () {
        console.log("登录时网络错误")
      }
    })

    var that = this;
    //获取外链内容
    wx.request({
      url: 'https://api.jiemengshi.com/wechat/dream/news/link',
      success: function (res) {
        console.log(res.data)
        var href = res.data.data;
        that.setData({
          hrefUrl: href.url,
          hrefDesc: href.description
        })
      }
    })    
  },

  gotoWebview: function (e) {
    var url = e.currentTarget.dataset.key;
    wx.navigateTo({ url: '../webview/webview?url=' + url })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;
    //获取公告内容
    wx.request({
      url: 'https://api.jiemengshi.com/wechat/dream/announcement/list',
      success: function (res) {
        console.log(res.data)
        //拼装数据
        var content = "";
        var index = 0;
        while (index < res.data.data.length) {
          content = content + res.data.data[index].content
          content = content + ";"
          index++
        }
        that.setData({
          boardContent : content
        })
      }
    })
  },

})
